"use strict";

(function($) {
  Drupal.behaviors.helper_comment_box = {
    attach: function(context, settings) {
      var $comment_box = $('.comment-box', context);

      if(!settings.user.uid) {
        $comment_box.autogrow();

        $('.comment-box-wrapper', context).unbind('click').click(function() {
          $('#modalLogin').modal('show');
          $comment_box.blur();
        });
      }
      else {
        $comment_box.once('comment_box', function() {
          $comment_box.autogrow();

          $comment_box.focus(function() {
            $(this).prev().hide();
          });

          $comment_box.blur(function() {
            if($(this).val().trim() == '') {
              $(this).prev().show();
            }
          });
        });

        $('.placeholder', context).once('placeholder', function() {
          $(this).unbind('click').click(function() {
            $(this).next().focus();
          });
        });

        $comment_box.unbind('keypress').keypress(function(e) {
          var $this = $(this);
          var $comment_wrapper = $this.parents('.node-nip').find('.comment-wrapper');
          var val = $this.val().trim();
          var nid = $this.data('nid');
          var view_mode = $this.data('view_mode');

          if ((e.keyCode === 13 || e.keyCode === 10) && !e.ctrlKey) {
            if(val == '') {
              // do nothing
            }
            else {
              $.post('/post-comment', {
                comment_body: val,
                nid: nid,
                view_mode: view_mode,
                crabii_app_token: Drupal.settings.crabii_app.crabii_app_token
              }, function(data) {
                var $data = $(data);
                $data.hide().appendTo($comment_wrapper).fadeIn();
                Drupal.attachBehaviors($data);
                $this.val('');

                // update placeholder if possible
                var $placeholder = $this.prev();

                if($placeholder.length) {
                  var placeholder_count = parseInt($placeholder.data('placeholder_count')) + 1;
                  $placeholder.data('placeholder_count', placeholder_count);
                  var placeholder_suffix = $placeholder.data('placeholder_suffix');
                  var placeholder_singular = $placeholder.data('placeholder_singular');
                  var placeholder_plural = $placeholder.data('placeholder_plural');
                  var placeholder_target = $placeholder.data('placeholder_target');
                  var placeholder = placeholder_count + ' ';

                  placeholder += placeholder_count == 1 ? placeholder_singular : placeholder_plural;
                  placeholder += ' ' + placeholder_suffix;
                  $placeholder.text(placeholder);

                  if(placeholder_target.length > 0) {
                    $(placeholder_target).text(placeholder_count);
                  }
                }
              });
              return false;
            }
          }
        });
      }
    }
  }
})(jQuery);
